import 'package:flutter/material.dart';

class SplashScreen extends StatefulWidget {
  final Widget childWidget;
  SplashScreen({Key key, @required this.childWidget}) : super(key: key);
  @override
  _SplashScreenState createState() => _SplashScreenState(this.childWidget);
}

class _SplashScreenState extends State<SplashScreen>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation _animation;
  final Widget childWidget;

  _SplashScreenState(this.childWidget);

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 3000),
    );
    _animation = Tween(begin: 0.0, end: 1.0).animate(_controller);
    _animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        // 动画结束
        Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(builder: (context) => childWidget),
            (route) => route == null);
      }
    });

    _controller.forward(); // 播放动画
  }

  @override
  void dispose() {
    _controller.dispose(); // 消除动画
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return FadeTransition(
      opacity: _animation,
      child: Image.network(
        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602307313809&di=9b71d342c4088c883b88038d66703da1&imgtype=0&src=http%3A%2F%2Fdesk.fd.zol-img.com.cn%2Fg5%2FM00%2F02%2F0B%2FChMkJlbK0ziIVblJAAEBizvNiyoAALJjAGneagAAQGj839.jpg',
        scale: 2.0,
        fit: BoxFit.fill,
      ),
    );
  }
}
